<template>
  <div>
    <el-card style="margin: 20px 0;">
      <CategorySelect @getCategoryId="getCategoryId" :show="!(scene == 0)"></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row, $index}">
              <el-button type="success" title="添加sku" icon="el-icon-plus" size="mini" @click="addSku(row)"></el-button>
              <el-button type="warning" title="修改spu" icon="el-icon-edit" size="mini"
                @click="updateSpu(row)"></el-button>
              <el-button type="info" title="查看当前spu列表" icon="el-icon-info" size="mini"
                @click="handler(row)"></el-button>
              <el-popconfirm :title="`确定删除${row.spuName}吗？`" @onConfirm="deleteSpu(row)">
                <el-button style="margin-left: 15px;" slot="reference" type="danger" title="删除spu" icon="el-icon-delete"
                  size="mini"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination style="text-align: center;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit"
          layout="prev, pager, next, jumper, ->,sizes, total" :total="total">
        </el-pagination>
      </div>
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu"></SpuForm>
      <SkuForm v-show="scene == 2" @changeScenes="changeScenes" ref="sku"></SkuForm>
    </el-card>
    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="skuList" style="width:100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="150"></el-table-column>
        <el-table-column prop="price" label="价格" width="200"></el-table-column>
        <el-table-column prop="weight" label="重量"></el-table-column>
        <el-table-column label="默认图片">
          <template slot-scope="{row, $index}">
            <img :src="row.skuDefaultImg" style="width: 100px;height: 100px">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './SpuForm/SpuForm.vue'
import SkuForm from './SkuForm/SkuForm.vue'

export default {
  name: 'Spu',
  data() {
    return {
      show: true,
      category1Id: '',
      category2Id: '',
      category3Id: '',
      page: 1,
      limit: 3,
      total: 0,
      records: [],
      scene: 0,// 0:列表数据 1:添加|修改 2:添加SKU
      dialogTableVisible: false,
      spu: {},
      skuList: [],
      loading: true
    }
  },
  components: { SpuForm, SkuForm },
  methods: {
    async getSpuList() {
      const { page, limit, category3Id } = this
      let res = await this.$API.spu.reqSpuList(page, limit, category3Id)
      if (res.code == 200) {
        this.records = res.data.records
        this.page = res.data.current
        this.limit = res.data.size
        this.total = res.data.total
      } else {

      }
    },
    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level == 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else if (level == 3) {
        this.category3Id = categoryId
        this.getSpuList()
      }
    },
    handleCurrentChange(page) {
      this.page = page
      this.getSpuList()
    },
    handleSizeChange(val) {
      this.limit = val
      this.page = 1
      this.getSpuList()
    },
    // 添加spu
    addSpu() {
      this.scene = 1
      this.$refs.spu.addSpuData(this.category3Id)
    },
    // 修改spu
    updateSpu(row) {
      this.scene = 1
      this.$refs.spu.initSpuData(row)
    },
    changeScene({ scene, flag }) {
      this.scene = scene
      if (flag == '添加') {
        this.page = 1
      }
      this.getSpuList()
    },
    changeScenes(scene) {
      this.scene = scene
    },
    async deleteSpu(row) {
      console.log(row);
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        if (this.records.length <= 1) this.page -= 1
        this.getSpuList()
      }
    },
    addSku(row) {
      this.scene = 2
      this.$refs.sku.getData(this.category1Id, this.category2Id, row)
    },
    async handler(row) {
      this.dialogTableVisible = true
      this.spu = row
      let result = await this.$API.sku.reqSkuListBySpuId(row.id)
      if (result.code == 200) {
        this.skuList = result.data
        this.loading = false
      }
    },
    close(done) {
      this.loading = true
      this.skuList = []
      done()
    }
  }
}
</script>

<style></style>